import { CALL_API } from 'redux-api-middleware'
import { extend } from 'lodash/fp'
import cookie from 'react-cookie'
import config from 'config'

// Constants
export const FETCH_ARTICLE_REQUEST = 'FETCH_ARTICLE_REQUEST'
export const FETCH_ARTICLE_SUCCESS = 'FETCH_ARTICLE_SUCCESS'
export const FETCH_ARTICLE_FAILURE = 'FETCH_ARTICLE_FAILURE'

export const FETCHFORUMTYPE_REQUEST = 'FETCHFORUMTYPE_REQUEST'
export const FETCHFORUMTYPE_SUCCESS = 'FETCHFORUMTYPE_SUCCESS'
export const FETCHFORUMTYPE_FAILURE = 'FETCHFORUMTYPE_FAILURE'

export const FILEUPLOAD_REQUEST = 'FILEUPLOAD_REQUEST'
export const FILEUPLOAD_SUCCESS = 'FILEUPLOAD_SUCCESS'
export const FILEUPLOAD_FAILURE = 'FILEUPLOAD_FAILURE'

export function fetchArticle (uid, c_id, a_name, content, imgArr) {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      credentials: "include",
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "addArticle",
        "params": [{
          "u_id": uid,
          "c_id": c_id,
          "a_name": a_name,
          "content": content,
          "content_img": imgArr
        }],
        "id": 1
      }),
      types: [
        FETCH_ARTICLE_REQUEST,
        {
          type: FETCH_ARTICLE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCH_ARTICLE_FAILURE
      ]
    }
  }
}

export function fetchForumType () {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/article.php?c=article',
      method: 'POST',
      body: JSON.stringify({
        "jsonrpc": "2.0",
        "method": "classification",
        "params": [],
        "id": 1
      }),
      types: [
        FETCHFORUMTYPE_REQUEST,
        {
          type: FETCHFORUMTYPE_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FETCHFORUMTYPE_FAILURE
      ]
    }
  }
}

export function fileUpload (fromData) {
  return {
    [CALL_API]: {
      endpoint: config.api_root_v1 + '/files.php?c=files&a=uploadImg',
      method: 'POST',
      body: fromData,
      types: [
        FILEUPLOAD_REQUEST,
        {
          type: FILEUPLOAD_SUCCESS,
          payload: (action, state, res) => {
            return res.json()
          }
        },
        FILEUPLOAD_FAILURE
      ]
    }
  }
}

export const actions = {
  fetchArticle,
  fetchForumType,
  fileUpload
}

// Action Handlers
const ACTION_HANDLERS = {
  [FETCH_ARTICLE_REQUEST]: (state, action) => {
    return ({ ...state, articleAct: { isFetching: true, error: {}, article: [] } })
  },
  [FETCH_ARTICLE_SUCCESS]: (state, action) => {
    return ({ ...state, articleAct: { isFetching: false, lastUpdate: Date.now(), article: action.payload } })
  },
  [FETCH_ARTICLE_FAILURE]: (state, action) => {
    return ({ ...state, articleAct: { isFetching: false, error: {}, article: [] } })
  },
  [FETCHFORUMTYPE_REQUEST]: (state, action) => {
    return ({ ...state, forumType: { isFetching: true, error: {}, type: {} } })
  },
  [FETCHFORUMTYPE_SUCCESS]: (state, action) => {
    return ({ ...state, forumType: { isFetching: false, error: {}, type: action.payload } })
  },
  [FETCHFORUMTYPE_FAILURE]: (state, action) => {
    return ({ ...state, forumType: { isFetching: false, error: {}, type: {} } })
  },
  [FILEUPLOAD_REQUEST]: (state, action) => {
    return ({ ...state, fileUpload: { isFetching: true, error: {}, upload: {} } })
  },
  [FILEUPLOAD_SUCCESS]: (state, action) => {
    return ({ ...state, fileUpload: { isFetching: false, error: {}, upload: action.payload } })
  },
  [FILEUPLOAD_FAILURE]: (state, action) => {
    return ({ ...state, fileUpload: { isFetching: false, error: {}, upload: {} } })
  }
}

// Reducers
const initialState = { articleAct: { isFetching: false, error: {}, article: [] }, forumType: { isFetching: true, error: {}, type: {} }, fileUpload: { isFetching: false, error: {}, type: {} } }
export default function articleReducer (state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type]
  return handler ? handler(state, action) : state
}
